from random import randrange

from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Map, Scatter, Funnel
from pyecharts.components import Table
from pyecharts.faker import Faker
from pyecharts.options import ComponentTitleOpts


def bar_base() -> Bar:
    c = (
        Bar().add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
            .add_yaxis("商家A", [randrange(0, 100) for _ in range(6)])
            .add_yaxis("商家B", [randrange(0, 100) for _ in range(6)])
            .set_global_opts(
            # title_opts=opts.TitleOpts(title="Bar-基本示例", title_link=url_for('index'), title_target='self',
            #                           subtitle="我是副标题")))   #title_link=url_for('index')  是连接
            title_opts=opts.TitleOpts(title="Bar-基本示例", title_target='self',
                                      subtitle="我是副标题")))
    return c


# def line_areastyle_boundary_gap() -> Line:
#     c = (Line().add_xaxis(Faker.choose()).add_yaxis(
#         "商家A", Faker.values(), is_smooth=True).add_yaxis(
#             "商家B", Faker.values(), is_smooth=True).set_series_opts(
#                 areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
#                 label_opts=opts.LabelOpts(is_show=False),
#             ).set_global_opts(
#                 title_opts=opts.TitleOpts(title="Line-面积图（紧贴 Y 轴）"),
#                 xaxis_opts=opts.AxisOpts(
#                     axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
#                     is_scale=False,
#                     boundary_gap=False,
#                 ),
#             ))
#     return c


def line_areastyle_boundary_gap() -> Line:
    c = (Line().add_xaxis(Faker.choose()).add_yaxis(
        "商家A", Faker.values(),
        areastyle_opts=opts.AreaStyleOpts(opacity=0.5)).add_yaxis(
        "商家B",
        Faker.values(),
        areastyle_opts=opts.AreaStyleOpts(opacity=0.5)).set_global_opts(
        title_opts=opts.TitleOpts(title="Line-面积图")))
    return c


def map_base() -> Map:
    data = [
        [10.0, 8.04],
        [8.0, 6.95],
        [13.0, 7.58],
        [9.0, 8.81],
        [11.0, 8.33],
        [14.0, 9.96],
        [6.0, 7.24],
        [4.0, 4.26],
        [12.0, 10.84],
        [7.0, 4.82],
        [5.0, 5.68],
    ]
    data.sort(key=lambda x: x[0])
    x_data = [d[0] for d in data]
    y_data = [d[1] for d in data]

    c = (
        Scatter(init_opts=opts.InitOpts(width="1600px", height="1000px"))
            .add_xaxis(xaxis_data=x_data)
            .add_yaxis(
            series_name="",
            y_axis=y_data,
            symbol_size=20,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_series_opts()
            .set_global_opts(
            title_opts=opts.TitleOpts(title="点阵图", subtitle="纯属虚构"),
            xaxis_opts=opts.AxisOpts(
                type_="value", splitline_opts=opts.SplitLineOpts(is_show=True)
            ),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            tooltip_opts=opts.TooltipOpts(is_show=True),
        )
    )
    return c


def line_set():
    import pyecharts.options as opts
    from pyecharts.charts import Line

    """
    Gallery 使用 pyecharts 1.1.0
    参考地址: https://www.echartsjs.com/examples/editor.html?c=line-marker
    
    目前无法实现的功能:
    
    1、最低气温的最高值暂时无法和 Echarts 的示例完全复刻
    """

    week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
    high_temperature = [11, 11, 15, 13, 12, 13, 10]
    low_temperature = [1, -2, 2, 5, 3, 2, 0]

    c = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
            .add_xaxis(xaxis_data=week_name_list)
            .add_yaxis(
            series_name="最高气温",
            y_axis=high_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
            .add_yaxis(
            series_name="最低气温",
            y_axis=low_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[
                    opts.MarkLineItem(type_="average", name="平均值"),
                    opts.MarkLineItem(symbol="none", x="90%", y="max"),
                    opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
                ]
            ),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="未来一周气温变化", subtitle="纯属虚构"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            # toolbox_opts=opts.ToolboxOpts(is_show=True),     # 工具
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    )
    return c


def funnel_set():
    c = (
        Funnel().add("商品", [list(z) for z in zip(Faker.choose(), Faker.values())], sort_="ascending",
                     label_opts=opts.LabelOpts(position="inside"), )
            .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Sort（ascending）"))
    )
    return c


def map_set():
    c = (
        Map()
            .add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
            .set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"))
    )
    return c

def line_base() -> Line:
    line = (
        Line()
        .add_xaxis(["{}".format(i) for i in range(10)])
        .add_yaxis(
            series_name="",
            y_axis=[randrange(50, 80) for _ in range(10)],
            is_smooth=True,
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="动态数据"),
            xaxis_opts=opts.AxisOpts(type_="value"),
            yaxis_opts=opts.AxisOpts(type_="value"),
        )
    )
    return line